/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <joerg@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.        Joerg Wunsch
 * ----------------------------------------------------------------------------
 *
 * HD44780 LCD display driver
 * 
 * ----------------------------------------------------------------------------
 * I extended and cleanuped a little this lcd libary but, as wish of Joerg the 
 * library still stay under "THE BEER-WARE LICENSE"  
 *                                                              Wojciech Todryk
 * ----------------------------------------------------------------------------
 * $Id: hd44780.h 46 2009-03-19 21:19:39Z musashi@todryk.pl $
 */

#ifndef HD44780_MODULE_H
#define HD44780_MODULE_H

/** @file hd44780.h
	@brief Plik nagłówkowy - Obsługa wyświetlacza LCD.
*/

#define HD44780_CONTROL_PORTOUT PORT(HD44780_CONTROL_PORT)
#define HD44780_CONTROL_PORTIN  PIN(HD44780_CONTROL_PORT)
#define HD44780_CONTROL_DDR     DDR(HD44780_CONTROL_PORT)

#define HD44780_DATA_PORTOUT    PORT(HD44780_DATA_PORT)
#define HD44780_DATA_PORTIN     PIN(HD44780_DATA_PORT)
#define HD44780_DATA_DDR        DDR(HD44780_DATA_PORT)

#define HD44780_BL_PORTOUT    PORT(HD44780_BL_PORT)
#define HD44780_BL_PORTIN     PIN(HD44780_BL_PORT)
#define HD44780_BL_DDR        DDR(HD44780_BL_PORT)

#define HD44780_DATABITS \
(_BV(HD44780_D4)|_BV(HD44780_D5)|_BV(HD44780_D6)|_BV(HD44780_D7))

#define HD44780_BUSYFLAG 0x80

/* Send a command to the LCD controller. */
#define hd44780_outcmd(n)	hd44780_outbyte((n), 0)

/* Send a data byte to the LCD controller. */
#define hd44780_outdata(n)	hd44780_outbyte((n), 1)

/* Read the address counter and busy flag from the LCD. */
#define hd44780_incmd()		hd44780_inbyte(0)

/* Read the current data byte from the LCD. */
#define hd44780_indata()	hd44780_inbyte(1)

#define hd44780_next_line() hd44780_goto(2,1)

#define hd44780_clear()		hd44780_outcmd(HD44780_CLR)
#define hd44780_home()		hd44780_outcmd(HD44780_HOME)
#define hd44780_cursor_on()	hd44780_outcmd(HD44780_DISPCTL(1, 1, 0))
#define hd44780_cursor_blink()	hd44780_outcmd(HD44780_DISPCTL(1, 1, 1))
#define hd44780_cursor_off()	hd44780_outcmd(HD44780_DISPCTL(1, 0, 0))

#define hd44780_bl_on()		HD44780_BL_PORTOUT |= _BV(HD44780_BL_SWITCH)
#define hd44780_bl_off()	HD44780_BL_PORTOUT &= ~_BV(HD44780_BL_SWITCH)

/* Clear LCD display command. */
#define HD44780_CLR \
	0x01

/* Home cursor command. */
#define HD44780_HOME \
	0x02

/*
 * Select the entry mode.  inc determines whether the address counter
 * auto-increments, shift selects an automatic display shift.
 */
#define HD44780_ENTMODE(inc, shift) \
	(0x04 | ((inc)? 0x02: 0) | ((shift)? 1: 0))

/*
 * Selects disp[lay] on/off, cursor on/off, cursor blink[ing]
 * on/off.
 */
#define HD44780_DISPCTL(disp, cursor, blink) \
	(0x08 | ((disp)? 0x04: 0) | ((cursor)? 0x02: 0) | ((blink)? 1: 0))

/*
 * With shift = 1, shift display right or left.
 * With shift = 0, move cursor right or left.
 */
#define HD44780_SHIFT(shift, right) \
	(0x10 | ((shift)? 0x08: 0) | ((right)? 0x04: 0))

/*
 * Function set.  if8bit selects an 8-bit data path, twoline arranges
 * for a two-line display, font5x10 selects the 5x10 dot font (5x8
 * dots if clear).
 */
#define HD44780_FNSET(if8bit, twoline, font5x10) \
	(0x20 | ((if8bit)? 0x10: 0) | ((twoline)? 0x08: 0) | \
		((font5x10)? 0x04: 0))

/*
 * Set the next character generator address to addr.
 */
#define HD44780_CGADDR(addr) \
	(0x40 | ((addr) & 0x3f))

/*
 * Set the next display address to addr.
 */
#define HD44780_DDADDR(addr) \
	(0x80 | ((addr) & 0x7f))

void hd44780_outbyte(uint8_t b, uint8_t rs);
uint8_t	hd44780_inbyte(uint8_t rs);
void hd44780_wait_ready(void);
void hd44780_init(void);
void hd44780_outstr(const char *s);
void hd44780_outstrn(const char *s);
void hd44780_outstrn_P(const char *s);
void hd44780_out8dec(const uint8_t i);
void hd44780_out8dec3(const uint8_t i);
void hd44780_out16dec(const uint16_t i);
void hd44780_out4hex(const uint8_t i);
void hd44780_out8hex(const uint8_t i);
void hd44780_out16hex(const uint16_t i);
void hd44780_out8bcd(const uint8_t i);
void hd44780_goto(uint8_t line, uint8_t pos);
void hd44780_printOnLcdDelay(const char *s);
void hd44780_switch_state(uint8_t,uint8_t);
void hd44780_label(const char *,uint8_t,uint8_t);

#endif
